const webpack = require('webpack');
const merge = require('webpack-merge');

module.exports = (deployEnvironment, webpackConfig) => {
    let globalVariables = ({
        dev: {
            appId: 'wxb7761aa9905ffb33',
            wechat: 'https://testcommon.jzdoctor.com/wechat',
            wechatOauth: 'https://testoauth.jzdoctor.com/wechat/authorize',
            appServer: 'https://test-api.jzdoctor.com',
            appAPI: 'https://niganmaa.com/',
        },
    
        stage: {
            appId: 'wxb7761aa9905ffb33',
            wechat: 'https://testcommon.jzdoctor.com/wechat',
            wechatOauth: 'https://testoauth.jzdoctor.com/wechat/authorize',
            appServer: 'https://test-api.jzdoctor.com',
            appAPI: 'https://niganmaa.com/',
        },
    
        prod: {
            appId: 'wxdec0babc72769ecc',
            wechat: 'https://common.jzdoctor.com/wechat',
            wechatOauth: 'https://oauth.jzdoctor.com/wechat/authorize',
            appServer: 'https://api.jzdoctor.com',
            appAPI: '//appapi.jzdoctor.com',
        }
    })[deployEnvironment];

    //  stringify the global variables value for DefinePlugin
    Object.keys(globalVariables).forEach(key => {
        globalVariables[key] = JSON.stringify(globalVariables[key]);
    });

    //  gen config
    let config = {
        plugins: [
            new webpack.DefinePlugin({
                DEPLOY_ENV: JSON.stringify(deployEnvironment),
                APP_CONFIG: {
                    ...globalVariables
                }
            })
        ]
    };

    return merge(config, webpackConfig);
};
